$(function () {
    initBanner();
    initPage();
    initNotice();
    initiRight();

});



/**
 * 查询banner
 */
function initBanner() {
    $.ajax({
        type: 'POST',
        url: '/blog/queryBannerList',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                var html = '';
                $.each(res.data,function (k,v) {
                    if(checkIsNullOrEmpty(v.jumpUrl)) {
                        html += '<div><a target="_blank" href="'+v.jumpUrl+'"><img src="'+v.imageUrl+'"></a></div>'
                    }else {
                        html += '<div><img src="'+v.imageUrl+'"></div>'
                    }
                })
                $('#blog-banner').html(html);

                //初始化banner
                layui.use('carousel', function(){
                    var carousel = layui.carousel;
                    //建造实例
                    carousel.render({
                        elem: '#blog-plan',
                        width: '100%' ,//设置容器宽度
                        height: '350px',
                        arrow: 'hover', //悬停显示箭头
                        anim: 'fade' //切换动画方式
                    });
                });

            }
        }
    });
}


/**
 * 初始化page
 * @param pageConf
 */
function initPage(pageConf) {
    if (!pageConf) {
        pageConf = {};
        pageConf.pageSize = 8;
        pageConf.currentPage = 1;
    }
    if(checkIsNullOrEmpty(getReqUrl('tagId'))) {
        pageConf.tagId = getReqUrl('tagId');
    }
    if(checkIsNullOrEmpty(getReqUrl('typeId'))) {
        pageConf.typeId = getReqUrl('typeId');
    }
    //调用后台
    $.ajax({
        type: 'POST',
        url: '/blog/queryBlogList',
        dataType: 'json',
        data: JSON.stringify(pageConf),
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                var total = res.data.total;
                layui.use('laypage',function () {
                    var laypage = layui.laypage;
                    laypage.render({
                        elem: 'page',
                        count: total,
                        curr: pageConf.currentPage,
                        limit: pageConf.pageSize,
                        first: '首页',
                        last: '尾页',
                        theme: '#284156',
                        layout: ['prev', 'page', 'next','skip'],
                        jump: function (obj, first) {
                            if (!first) {
                                pageConf.currentPage = obj.curr;
                                pageConf.pageSize = obj.limit;
                                initPage(pageConf);
                            }
                        }
                    });
                });
                //清空内容填充 填充内容
                $('#blog-left-content').html('');
                var artictleData = res.data.records;
                if(total > 0) {
                    var ARTIC_HTML = showArtictle(artictleData);
                    $('#blog-left-content').append(ARTIC_HTML);
                    //关键词高亮
                    $('#blog-left-content').GL({
                        ocolor: 'red',//设置关键词高亮颜色
                        oshuru: pageConf.keyWords//设置要显示的关键词
                    });
                }else {
                    var nodata = '';
                    nodata += '<div id="nodata" style="height: 100%;background: #FFFFFF;box-shadow: 0 2px 4px 0 rgba(0,0,0,0.05);display: flex;justify-content: center;align-items: center;">';
                    nodata += '<img src="/images/nodata.png" alt="" ></div>';
                    $('#blog-left-content').append(nodata);
                }


            }
        },
        error: function() {
            layer.alert()
        }
    });
}

/**
 * 初始化公告
 */
function initNotice() {
    $.ajax({
        type: 'POST',
        url: '/blog/queryBlogNotice',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                var html = '';
                $.each(res.data,function (k,v) {
                   var noticeUrl = v.noticeUrl;
                   if(checkIsNullOrEmpty(noticeUrl)) { //不为空
                       html += '<span style="color: '+v.noticeCss+'">'+v.noticeName+'<a href="'+noticeUrl+'" target="_blank" style="color:#FF5100">&nbsp;&nbsp;详情</a></span>';
                   }else {
                       html += ' <span style="color: '+v.noticeCss+'">'+v.noticeName+'</span>';
                   }
                })
                $('.home-tips-container').html(html);
                playNotice(5000);
            }
        },
        error: function() {
            layer.alert()
        }
    });
}

/**
 * 加载右侧侧边栏信息
 */
function initiRight() {
    //调用后台
    $.ajax({
        type: 'POST',
        url: '/blog/queryRightInfo',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                $.each(res.data.userBeanList,function (index,bloggerData) {
                     initBlogger(bloggerData); //个人信息
                })

                initRank(res.data.rankBeanList); //排行榜信息

                initTag(res.data.tagList); //标签墙信息
            }
        },
        error: function() {
            layer.alert()
        }
    });

}

//初始化个人信息
function initBlogger(bloggerData) {
    var html = '';
    html += '<div class="blogerinfo shadow">';
    html += '<div class="blogerinfo-figure">';
    html += '<img src="'+bloggerData.userPhoto+'" style="width: 60%;height: 15%" alt="'+bloggerData.nickName+'" />';
    html += '</div>';
    html += '<p class="blogerinfo-nickname">'+bloggerData.nickName+'</p>';
    html += '<p class="blogerinfo-introduce">'+bloggerData.userIntroduce+'</p>';
    html += '<p class="blogerinfo-location"><i class="fa fa-location-arrow"></i>&nbsp;'+bloggerData.userAddr+'</p>';
    html += '<hr />';
    html += '<div class="blogerinfo-contact">';
    $.each(bloggerData.bloggerList,function (k,v) {
        if(v.linkClass.indexOf('fa-weixin') >= 0) {
            html += '<a class="social weixin" href="javascript:"><img class="qrcode" src="'+v.linkUrl+'" alt="'+v.linkType+'"><i class="'+v.linkClass+' weixin"></i></a>';
        }else {
            html += '<a target="_blank" title="'+v.linkType+'" href="'+v.linkUrl+'"><i class="'+v.linkClass+'"></i></a>';
        }
    })
    html += '</div>';
    html += '</div>';
    //加载个人信息
    $('.blog-main-right').append(html);
}

//初始化热文信息
function initRank(rankData) {
    var html = '';
    html += '<div class="blog-module shadow">';
    html += '<div class="blog-module-title">最热阅读</div>';
    html += '<ul class="fa-ul blog-module-ul">';
    $.each(rankData,function (k,v) {
       html += '<li><i class="fa-li fa fa-hand-o-right"></i><a href="detail.html?id='+v.id+'">'+v.title+'</a></li>';
    });
    html += '</ul>';
    html += '</div>';
    //加载热文信息
    $('.blog-main-right').append(html);
}

//初始化标签墙信息
function initTag(tagData) {
    var html = '';
    html += '<div class="cloud">';
    html += '<h2 class="cloud-title">标签墙</h2>';
    html += '<ul>';
    $.each(tagData,function (k,v) {
        html += '<a  href="index.html?tagId='+v.tagId+'" title="'+v.tagName+'">'+v.tagName+'('+v.tagCnt+')</a>';
    })
    html += '</ul>';
    html += '</div>';
    //加载标签墙信息
    $('.blog-main-right').append(html);
}
    
/**
 * 生成文章列表
 * @param artictleData
 */
function showArtictle(artictleData) {
    var ARTIC_HTML = '';
    $.each(artictleData,function (index,val) {
        ARTIC_HTML += '<div class="article shadow">';
        ARTIC_HTML += '<div class="article-left">';
        ARTIC_HTML += '<a href="detail.html?id='+val.id+'"><img src="'+val.coverPhoto+'" alt="'+val.title+'" /></a>';
        ARTIC_HTML += '</div>';
        ARTIC_HTML += '<div class="article-right">';
        ARTIC_HTML += '<div class="article-title">';
        ARTIC_HTML += '<a href="detail.html?id='+val.id+'">'+val.title+'</a>';
        ARTIC_HTML += '</div>';
        ARTIC_HTML += '<div class="article-abstract">'+val.summary+'';
        ARTIC_HTML += '<a href="/detail.html?id='+val.id+'">查看更多</a>'
        ARTIC_HTML += '</div>';
        ARTIC_HTML += '</div>';
        ARTIC_HTML += '<div class="clear"></div>';
        ARTIC_HTML += '<div class="article-footer">';
        ARTIC_HTML += '<span><i class="fa fa-clock-o"></i>发表于&nbsp;'+val.releaseDate+'</span>';
        ARTIC_HTML += '<span class="article-author"><i class="fa fa-user"></i>作者&nbsp;&nbsp;sunny</span>';
        ARTIC_HTML += '<span><i class="fa fa-tag"></i>分类&nbsp;<a  href="index.html?typeId='+val.typeId+'">'+val.typeName+'</a></span>';
        ARTIC_HTML += '<span class="article-viewinfo"><i class="fa fa-eye"></i>阅读&nbsp;('+val.browseNum+')</span>';
        ARTIC_HTML += '<span class="article-viewinfo"><i class="fa fa-commenting"></i>评论&nbsp;('+val.commentNum+')</span>';
        ARTIC_HTML += '</div>';
        ARTIC_HTML += '</div>';
    });

    return ARTIC_HTML;
}